<template>
  <div v-loading="loading" class="padding">
    <ApplyReport :nowRowData="nowRowData" :creditCode="nowRowData.creditCode">
      <el-scrollbar max-height="50vh" v-if="lineTimeList.length > 0">
        <div class="progress-box">
          <div class="time-line">
            <div class="time-line-item" v-for="(item, index) in lineTimeList" :key="index">
              <div class="time-line-item-header flex space-between align-center">
                <p class="text-time">{{ item.createTime ? formatCreateTime(item.createTime) : '待审批节点' }}</p>
                <div :class="item.approvalStatus == 3 ? 'text-error-tag' : 'text-success-tag'" v-if="item.statusLabel">{{
                  item.statusLabel }}</div>
              </div>
              <p class="text-title">{{ approvalHierarchyLabel[item.approvalHierarchy] || item.createUserName }} </p>
              <p class="text-value">{{ item.approvalRemarks }}</p>
              <p class="text-value" v-if="item.currentApproval">节点审批人：{{ item.currentApprovalUserName }}</p>
              <FileList v-if="item.approvalImgs" :value="item.approvalImgs" class="mt10" />
            </div>
          </div>
        </div>
      </el-scrollbar>
    </ApplyReport>
    <div class="footer-btn fc mt10" v-if="nowRowData.approvalStatus == 1 || nowRowData.approvalStatus == -1">
      <el-button type="primary" :disabled="loading" @click="handleSubmitApply">提交</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import ApplyReport from '../RuleForm/ApplyReport/index.vue'
import { onMounted } from 'vue'
import FileList from '@/views/aplid/DetailPage/ContentListItem/FileList/index.vue'
import { useDateFormat } from '@vueuse/core'
import bus from '@/utils/eventbus'

const props = defineProps<{
  nowRowData: any
}>()

const loading = ref(false)
// 从业人员提交时，判断表格有没有证书
let EntryListData = null
bus.on('waitSubmitEntryListData', (data) => {
  EntryListData = data
})
let PostListData = null
bus.on('waitSubmitPostListData', (data) => {
  PostListData = data
})

const emit = defineEmits(['refresh'])
const statusLabel = {
  '-1': '作废',
  '0': '待审批',
  '1': '审批中',
  '2': '通过',
  '3': '驳回',
}
const approvalHierarchyLabel = {
  '1': '社区',
  '2': '街道',
  '3': '区',
  '4': '市',
  '5': '第三方',
}

const lineTimeList = ref([])

const formatCreateTime = (time) => {
  const ntme = useDateFormat(time, 'YYYY年MM月DD日 HH:mm:ss')
  return ntme.value
}

const getInitData = () => {
  let params = {
    fromId: props.nowRowData.id,
    randomNo: props.nowRowData.workflowRandom,
  }

  apiMod.view_workflowApprovalLogic_process(params).then((res) => {
    bus.emit('processData', res.data)
    lineTimeList.value = (res.data || []).map((item) => {
      return {
        ...item,
        statusLabel: statusLabel[item.approvalStatus],
      }
    }).reverse()
  })
}

const handleSubmitApply = () => {
  
  // 判断从业人员补贴有没有证书
  if (props.nowRowData.subsidyType == 5) {
    // 入职补贴
    if (!EntryListData || (!EntryListData.qualificationCertificateName)) {
      ElMessage.error('该人员没有相关证书，不可提交')
      return
    }
  }
  if (props.nowRowData.subsidyType == 10) {
    // 岗位津贴
    if (!PostListData || !PostListData.qualificationCertificateNameAndGrade) {
      ElMessage.error('该人员没有相关证书，不可提交')
      return
    }
  }
  const params = {
    fromId: props.nowRowData.id,
    randomNo: props.nowRowData.workflowRandom,
    areaId: props.nowRowData.areaId,
  }

  loading.value = true 
  apiMod.view_workflowApprovalLogic_submit(params).then(res => {
    ElMessage.success(res.data)
    emit('refresh')
    loading.value = false
  }).catch(()=> {
    loading.value = false
  })
}

onMounted(() => {
  getInitData()
})
</script>

<style scoped lang="scss">
@import '@/views/instOperate/components/styles/progress.scss';
</style>
